from django.shortcuts import render, redirect
from app01 import models


# Create your views here.
def add_publisher(request):
    if request.method == 'POST':
        # 获取表单提交的内容
        publisher_name = request.POST.get('name')
        publisher_address = request.POST.get('address')
        print(publisher_name, publisher_address)
        # 保存到数据库中
        models.Publisher.objects.create(name=publisher_name, address=publisher_address)
        # redirect()重定向
        return redirect("/app01/publisher_list")
    return render(request, "add_publisher.html")


def publisher_list(request):
    # 查询数据库中所有信息
    publisher_list = models.Publisher.objects.all()
    return render(request, "publisher_list.html", {"publisher_list": publisher_list})


def edit_publisher(request):
    if request.method == 'POST':
        # 获取表单提交的内容
        id = request.POST.get('id')
        publisher_name = request.POST.get('name')
        publisher_address = request.POST.get('address')
        # 根据id去数据库查找数据
        publisher_obj = models.Publisher.objects.get(id=id)
        # 修改
        publisher_obj.name = publisher_name
        publisher_obj.address = publisher_address
        publisher_obj.save()
        # redirect()重定向
        return redirect("/app01/publisher_list")
    # 获取id
    id = request.GET.get('id')
    # 去数据库查询对应数据
    publisher_obj = models.Publisher.objects.get(id=id)
    publisher_obj_list = models.Publisher.objects.all()
    return render(request, "edit_publisher.html", {"publisher_obj": publisher_obj,
                                                   "publisher_obj_list": publisher_obj_list})


def delete_publisher(request):
    id = request.GET.get('id')
    publish_obj = models.Publisher.objects.filter(id=id).delete()
    return redirect("/app01/publisher_list")


def book_list(request):
    # 获取图书信息
    book_obj_list = models.Book.objects.all()
    return render(request, "book_list.html", {"book_obj_list": book_obj_list})


def add_book(request):
    if request.method == 'POST':
        # 获取表单提交的内容
        name = request.POST.get('name')
        address = request.POST.get('address')
        price = request.POST.get('price')
        inventory = request.POST.get('inventory')
        sale_num = request.POST.get('sale_num')
        publisher_id = request.POST.get('publisher_id')
        # 保存到数据库中
        models.Book.objects.create(name=name, price=price, inventory=inventory, sale_num=sale_num
                                   , publisher_id=publisher_id)
        # redirect()重定向
        return redirect("/app01/book_list")
    publisher_obj_list = models.Publisher.objects.all()

    return render(request, "add_book.html", {"publisher_obj_list": publisher_obj_list})


def edit_book(request):
    if request.method == 'POST':
        # 获取表单提交的内容
        id = request.POST.get('id')
        name = request.POST.get('name')
        price = request.POST.get('price')
        inventory = request.POST.get('inventory')
        sale_num = request.POST.get('sale_num')
        publisher_id = request.POST.get('publisher_id')

        models.Book.objects.filter(id=id).update(name=name, price=price, inventory=inventory, sale_num=sale_num
                                                 , publisher_id=publisher_id)
        return redirect('/app01/book_list')
    #   获取级联的出版社地址
    publisher_obj_list = models.Publisher.objects.all()
    id = request.GET.get('id')
    book_obj = models.Book.objects.get(id=id)
    return render(request, "edit_book.html", {"book_obj": book_obj, "publisher_obj_list": publisher_obj_list})


def delete_book(request):
    id = request.GET.get('id')
    models.Book.objects.filter(id=id).delete()
    return redirect('/app01/book_list')


def add_author(request):
    if request.method == 'POST':
        name = request.POST.get('name')
        book_ids = request.POST.getlist('books')
        author_obj = models.Author.objects.create(name=name)  # 创建对象
        author_obj.book.set(book_ids)  # 设置关系
        return redirect("/app01/author_list")
    # 1、获取所有图书
    book_obj_list = models.Book.objects.all()
    # 2、返回页面
    return render(request, 'add_author.html', {"book_obj_list": book_obj_list})


def author_list(request):
    ret_list = []
    author_obj_list = models.Author.objects.all()
    for author_obj in author_obj_list:
        book_obj_list = author_obj.book.all()
        ret_dic = {}
        ret_dic['author_obj'] = author_obj
        ret_dic['book_list'] = book_obj_list
        ret_list.append(ret_dic)
    return render(request, 'author_list.html', {"ret_list": ret_list})


def edit_author(request):
    if request.method == 'POST':
        id = request.POST.get('id')
        name = request.POST.get('name')
        books = request.POST.getlist('books')
        author_obj = models.Author.objects.filter(id=id).first()
        author_obj.name = name
        author_obj.book.set(books)
        author_obj.save()
        return redirect('/app01/author_list')
    id = request.GET.get('id')
    author_obj = models.Author.objects.get(id=id)
    book_obj_list = models.Book.objects.all()
    return render(request,"edit_author.html",{"author_obj":author_obj,"book_obj_list":book_obj_list})


def delete_author(request):
    id = request.GET.get('id')
    models.Author.objects.filter(id=id).delete()
    return redirect('/app01/author_list')
